//
//  KZTestResultInfomationView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/10/24.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZTestResultInfomationView.h"

#import "KZShadowButton.h"

@implementation KZTestResultInfomationView
{
    UIView *_bgView;            // 总背景
    UIView *_shadowView;        // 阴影
    UILabel *_titleLabel;       // 通过与否
    UIView *_infomationView;    // 信息
    
    UILabel *_rightCountLabel;  // 正确
    UILabel *_wrongCountLabel;  // 错误
    UILabel *_useTimeLabel;     // 用时
    
    UIButton *_button1;
    KZShadowButton *_button2;
}
- (instancetype)init {
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _bgView = UIView.new;
    _bgView.backgroundColor = UIColor.whiteColor;
    _bgView.layer.cornerRadius = 4;
    _bgView.layer.masksToBounds = true;
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.bottom.equalTo(self.mas_centerY).multipliedBy(0.6);
    }];
    
    _shadowView = UIView.new;
    _shadowView.layer.cornerRadius = 4;
    _shadowView.backgroundColor = UIColor.lightGrayColor;
    _shadowView.layer.shadowColor = UIColor.grayColor.CGColor;
    _shadowView.layer.shadowRadius = 10;
    _shadowView.layer.shadowOpacity = 0.6;
    _shadowView.layer.shadowOffset = CGSizeMake(0, 2);
    [self insertSubview:_shadowView atIndex:0];
    [_shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_bgView);
    }];
    
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.adjustsFontSizeToFitWidth = true;
    _titleLabel.font = FONT(H(20));
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [KZThemeColor colorWithAlphaComponent:0.8];
    [_bgView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(-40);
    }];
    
    _infomationView = UIView.new;
    [_bgView addSubview:_infomationView];
    [_infomationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_bgView);
        make.centerY.equalTo(_bgView);
        make.height.mas_equalTo(H(100));
    }];
    
    [self setupInfomationViews];
    
    _button2 = [[KZShadowButton alloc] init:^(UIButton *button) {
        button.titleLabel.font = FONT(18);
        [button setTitle:@"下一个词汇包" forState:UIControlStateNormal];
    }];
    _button2.alpha = 0;
    [_button2 addTarget:self action:@selector(action2) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_button2];
    [_button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_bgView);
        make.width.equalTo(_bgView).multipliedBy(0.8);
        make.height.mas_equalTo(H(40));
        make.bottom.equalTo(_bgView).offset(H(-30));
    }];
    
    _button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    _button1.alpha = 0;
    _button1.titleLabel.font = FONT(18);
    _button1.layer.cornerRadius = H(20);
    _button1.layer.masksToBounds = true;
    _button1.layer.borderColor = [KZThemeColor colorWithAlphaComponent:0.6].CGColor;
    _button1.layer.borderWidth = 1;
    [_button1 setTitle:@"查看不合格词汇" forState:UIControlStateNormal];
    [_button1 setTitleColor:KZThemeColor forState:UIControlStateNormal];
    [_button1 addTarget:self action:@selector(action1) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_button1];
    [_button1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.height.width.equalTo(_button2);
        make.bottom.equalTo(_button2.mas_top).offset(H(-30));
    }];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self startAnimation];
    });
}
- (void)setupInfomationViews {
    
    NSArray *titles = @[@"合格", @"不合格", @"用时"];
    NSArray *colors = @[
                        [UIColor colorWithR:81 g:209 b:64 a:1],
                        [UIColor colorWithR:250 g:182 b:72 a:1],
                        [UIColor colorWithR:253 g:141 b:85 a:1],
                        ];
    NSArray *colors2 = @[
                        [UIColor colorWithR:153 g:241 b:144 a:1],
                        [UIColor colorWithR:253 g:219 b:111 a:1],
                        [UIColor colorWithR:251 g:182 b:141 a:1],
                        ];
    UIView *last = nil;
    for (NSInteger i = 0; i < titles.count; i ++) {
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.adjustsFontSizeToFitWidth = true;
        titleLabel.numberOfLines = 1;
        titleLabel.text = titles[i];
        titleLabel.font = FONT(H(15));
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor blackColor];
        [_infomationView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(H(30));
            make.width.equalTo(_infomationView).multipliedBy(1.0/3);
            make.bottom.equalTo(@0);
            if (last) {
                make.left.equalTo(last.mas_right);
            } else {
                make.left.mas_equalTo(0);
            }
        }];
        last = titleLabel;
        
        UIControl *gradientView = [UIControl new];
        [_infomationView addSubview:gradientView];
        CGFloat width = H(60);
        gradientView.layer.cornerRadius = width / 2;
        gradientView.layer.masksToBounds = true;
        [gradientView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(titleLabel);
            make.width.height.mas_equalTo(width);
            make.top.mas_equalTo(H(10));
        }];
        // 渐变图层
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.bounds = CGRectMake(0, 0, width, width);
        gradientLayer.position = CGPointMake(width/2, width/2);
        [gradientLayer setStartPoint:CGPointMake(0, 0)];
        [gradientLayer setEndPoint:CGPointMake(1, 1)];
        UIColor *color = colors[i];
        UIColor *color2 = colors2[i];
        gradientLayer.colors = @[
                                 (__bridge id)color.CGColor,
                                 (__bridge id)color2.CGColor,
                                 ];
        [gradientView.layer insertSublayer:gradientLayer atIndex:0];
        
        UILabel *label = nil;
        if (i != 2) {
            label = [[UILabel alloc] init];
            label.adjustsFontSizeToFitWidth = true;
            label.numberOfLines = 1;
            label.text = @"0";
            label.font = FONT(H(18));
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor whiteColor];
            [gradientView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(gradientView);
            }];
        } else {
            UIImageView *imageView = [UIImageView new];
            imageView.image = [UIImage imageNamed:@"kz-用时"];
            [gradientView addSubview:imageView];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(gradientView.mas_centerY);
                make.height.equalTo(gradientView).multipliedBy(0.5*0.6);
                make.width.equalTo(imageView.mas_height).multipliedBy(69.0/41);
                make.centerX.equalTo(gradientView);
            }];
            
            label = [[UILabel alloc] init];
            label.adjustsFontSizeToFitWidth = true;
            label.numberOfLines = 1;
            label.text = @"0";
            label.font = FONT(H(13));
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor whiteColor];
            [gradientView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(imageView.mas_bottom).offset(H(2));
                make.centerX.equalTo(gradientView);
            }];
        }
        switch (i) {
            case 0:
                _rightCountLabel = label;
                break;
            case 1:
                _wrongCountLabel = label;
                break;
            case 2:
                _useTimeLabel = label;
                break;
                
            default:
                break;
        }
    }
}

- (void)setPackageModel:(KZVocabularyPackageModel *)packageModel {
    
    _packageModel = packageModel;
    
    NSInteger successCount = 0;
    NSInteger failureCount = 0;
    for (KZVocabularyCurrentPackageDetailModel *model in _packageModel.currentPackage.Detail) {
        if (model.IsPassed.boolValue == false) {
            failureCount ++;
        } else {
            successCount ++;
        }
    }
    
    int time = _packageModel.currentPackage.UsedTime.intValue;
    
    _rightCountLabel.text = [NSString stringWithFormat:@"%li", successCount];
    _wrongCountLabel.text = [NSString stringWithFormat:@"%li", failureCount];
    _useTimeLabel.text = [NSString stringWithFormat:@"%02i:%02i", (int)time/60, (int)time%60];

    // 4星
    if (failureCount >= 0 && failureCount < 4) {
        _titleLabel.text = @"太棒了，继续保持哦！";
    }
    // 1-3星
    else if (failureCount >= 4 && failureCount < 16) {
        _titleLabel.text = @"加油，做的不错哦！";
    }
    // 0星
    else {
        _titleLabel.text = @"很遗憾，再接再厉哦！";
    }
    // 根据错题
    if (failureCount > 0) {
        [_button1 setTitle:@"查看不合格词汇" forState:UIControlStateNormal];
    } else {
        [_button1 setTitle:@"关闭窗口" forState:UIControlStateNormal];
    }
}

- (void)setResultDic:(NSDictionary *)resultDic {
    
    _resultDic = resultDic;
    NSInteger successCount = ((NSNumber*)[_resultDic objectForKey:@"TrueCount"]).integerValue;
    NSInteger failureCount = ((NSNumber*)[_resultDic objectForKey:@"WrongCount"]).integerValue;
    
    int time = [_resultDic[@"time"] intValue];
    
    _rightCountLabel.text = [NSString stringWithFormat:@"%li", successCount];
    _wrongCountLabel.text = [NSString stringWithFormat:@"%li", failureCount];
    _useTimeLabel.text = [NSString stringWithFormat:@"%02i:%02i", (int)time/60, (int)time%60];
    
    if (failureCount > 0) {
        [_button1 setTitle:@"查看错题" forState:UIControlStateNormal];
        _titleLabel.text = @"很遗憾，本轮训练不通过！";
         [_button2.button setTitle:@"重新闯关" forState:UIControlStateNormal];
    } else {
        [_button1 setTitle:@"关闭窗口" forState:UIControlStateNormal];
        _titleLabel.text = @"恭喜你，本轮训练过关啦！";
        [_button2.button setTitle:@"下一关" forState:UIControlStateNormal];
    }
    
   
    BOOL isLastTure = [_resultDic[@"isLastTure"] boolValue];
    if (isLastTure) {
        _button2.userInteractionEnabled = false;
        _button2.colors = @[[UIColor colorWithWhite:0.9 alpha:1],
                            [UIColor colorWithWhite:0.9 alpha:1],];
    }
}


- (void)startAnimation {
    
    [UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [_bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(0);
        }];
        [_titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_bgView).offset(H(60));
        }];
        [_infomationView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_bgView).offset(-H(60));
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            _button1.alpha = 1;
            _button2.alpha = 1;
        }];
    }];
}

#pragma mark - 事件
- (void)action1 {
    
    if ([_button1.titleLabel.text isEqualToString:@"查看不合格词汇"]||[_button1.titleLabel.text isEqualToString:@"查看错题"]) {
        _clickBlock(1);
    } else {
        _clickBlock(2);
    }
}
- (void)action2 {
    _clickBlock(0);
}

@end
